﻿

$(document).ready(function () {
    window.submitForm = [];
    $('form[validate]').each(function (i) {
        $(this).attr("onsubmit", "return window.submitForm[" + i + "](" + i + ")");
        window.submitForm.push(function (i) {
            var items = $('form[validate]').eq(i).find('input[validate],select[validate],textarea[validate]');
            for (var i = 0; i < items.length; i++) {
                var val = $(items[i]).val();
                var validatefiled = $(items[i]).attr('validatefiled');
                var filed = validatefiled ? validatefiled : $(items[i]).attr('placeholder');

                var validatefor = $(items[i]).attr('validatefor');
                var isCheck = true;
                if (validatefor && validatefor.length > 0) {
                    isCheck = $('#' + validatefor).is(":checked");
                }
                if (!validatefor || isCheck) {
                    if (val.length == 0) {
                        show_message(filed + "不可为空", "warning");
                        return false;
                    }
                    //正则表达式验证
                    var regular = $(items[i]).attr('regular');
                    if (regular == "tel") {
                        if (!(/^1[3|4|5|7|8][0-9]\d{8}$/.test(val))) {
                            show_message(filed + '格式不正确', "warning");
                            return false;
                        }
                    }
                    //比较验证
                    var compare = $(items[i]).attr('compare');
                    if (compare) {
                        if (val != $('#' + compare).val()) {
                            var validatefiled2 = $('#' + compare).attr('validatefiled');
                            var filed2 = validatefiled2 ? validatefiled2 : $('#' + compare).attr('placeholder');
                            show_message(filed + "与" + filed2 + '不匹配', "warning");
                            return false;
                        }
                    }
                }
            }
        })
    });
});
